package com.hzz.mall.seckill.controller;

import com.atguigu.common.utils.R;
import com.hzz.mall.seckill.service.SeckillService;
import com.hzz.mall.seckill.to.SecKillSkuRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 胡忠智
 * @date 2021/12/26-19:03
 * @Description TODO
 */
@Slf4j
@Controller
public class SeckillController {
    @Autowired
    SeckillService seckillService;

    @GetMapping(value = "/getCurrentSeckillSkus")
    @ResponseBody // 用户网页发请求
    public R getCurrentSeckillSkus() {
        //(重要)获取到当前时间可以参加秒杀商品的信息
        log.info("getCurrentSeckillSkus正在执行。。。。");
        List<SecKillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    @ResponseBody
    @GetMapping(value = "/getSeckillSkuInfo/{skuId}")
    public R getSeckillSkuInfo(@PathVariable("skuId") Long skuId) {
        SecKillSkuRedisTo to = seckillService.getSeckillSkuInfo(skuId);
        return R.ok().setData(to);
    }

    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId, // session_skuID
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model){
        String orderSn = seckillService.kill(killId,key,num);
        // 1.判断是否登录
        model.addAttribute("orderSn", orderSn);
        return "success";
    }

}
